﻿using System;
using System.Data.SqlClient;
using System.Linq;
using System.Web.Mvc;
using Microsoft.Web.Mvc;
using SapaFinance.DTO;
using SapaFinance.Services;
using Telerik.Web.Mvc;

namespace SapaFinance.UI.Controllers
{
    public class AccountController : Controller
    {
        private readonly IAccountService _accountService;
        private readonly ICorporationService _corporationService;

        public AccountController(IAccountService accountService, ICorporationService corporationService)
        {
            _accountService = accountService;
            _corporationService = corporationService;
        }

        public virtual ActionResult Index()
        {
            return View("Index", _accountService.GetAccountList());
        }

        public virtual ActionResult GetAccountSelectedList(Guid id)
        {
            var corp = _corporationService.GetCorporation(id);
            var account = corp.GetPrimaryAccount().UID;
            var accountDetailsDTO = _accountService.GetAccountSelectedList(account);
            return PartialView("EditorTemplates/AccountList", accountDetailsDTO);
        }

        //public virtual ActionResult GetAccountDetails(Guid id)
        //{
        //    var currencyDetailDTO = _accountService.GetCurrencyDetailByAccountId(id);
        //    return PartialView("EditorTemplates/CurrencyDetailDTO", currencyDetailDTO);
        //}

        private void ValidateDto(AccountDTO dto)
        {
            if (dto.SelectedBank.Equals(Guid.Empty)) ModelState.AddModelError("SelectedBank", "Банк не выбран");
            if (dto.SelectedCorporation.Equals(Guid.Empty)) ModelState.AddModelError("SelectedCorporation", "Юр.лицо не выбрано");
            if (dto.SelectedCurrency.Equals(Guid.Empty)) ModelState.AddModelError("SelectedCurrency", "Валюта не выбрана");
        }

        [HttpPost]
        [GridAction]
        public virtual ActionResult Insert(AccountDTO dto)
        {
            dto.CurrentUser = User.Identity.Name;
            ValidateDto(dto);
            if (ModelState.IsValid)
            {
                _accountService.CreateAccount(dto);
                return this.RedirectToAction(x => x.Index());
            }
            return Index();
        }

        [HttpPost]
        [GridAction]
        public virtual ActionResult Delete(Guid id)
        {
            //_accountService.DeleteAccount(id);
            _corporationService.DeleteAccount(id);
            return this.RedirectToAction(x => x.Index());
        }

        [HttpPost]
        [GridAction]
        public virtual ActionResult Update(AccountDTO dto)
        {
            dto.CurrentUser = User.Identity.Name;
            ValidateDto(dto);
            if (ModelState.IsValid)
            {
                _accountService.UpdateAccount(dto);
                return this.RedirectToAction(x => x.Index());
            }
            return Index();
        }
    }
}